package com.sanjiahantang.youjiarole.module.common.holders;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyGridView;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.common.adapters.ShopAdapter;
import com.sanjiahantang.youjiarole.module.common.beans.ShopBean;
import com.sanjiahantang.youjiarole.module.goods.views.impl.GoodsDetailActivity;
import com.sanjiahantang.youjiarole.module.goods.views.impl.GoodsListActivity;
import com.sanjiahantang.youjiarole.module.match.beans.MatchListBean;
import com.sanjiahantang.youjiarole.module.match.views.impl.MatchActivity;
import com.sanjiahantang.youjiarole.module.match.views.impl.MatchListActivity;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.CommonPagerTitleView;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zy on 2017/8/24.
 */

public class ShopHolder extends RecyclerView.ViewHolder implements StaticValue{


    //各种类型
    public ShopHolder(Activity activity, View itemView, int type, Object data) {
        super(itemView);
        switch (type){
            case ShopAdapter.TYPE_STYLE:
                initStyle(activity,itemView,(List<ShopBean.Category>) data);
                break;
            case ShopAdapter.TYPE_COLLOCATION:
                initCollocationItem(activity,itemView,(MatchListBean.Detail)data);
                break;
            case ShopAdapter.TYPE_RECOMMEND:
                initRecommend(activity,itemView,(ShopBean.Category)data);
                break;
            case ShopAdapter.TYPE_COLLOCATION_TITLE:
                initCollocationTitle(activity,itemView);
                break;
        }
    }

    private void initCollocationTitle(final Activity activity, View itemView) {
        itemView.findViewById(R.id.vRight).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.startActivity(new Intent(activity, MatchListActivity.class));
            }
        });
    }

    private void initRecommend(final Activity activity, View itemView, final ShopBean.Category category) {
        TextView txtName = (TextView) itemView.findViewById(R.id.txtTitle);
        txtName.setText(category.getName());
        View tv_more = itemView.findViewById(R.id.tv_more);
        tv_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (category.getItemList().size() > 0){
                    activity.startActivity(new Intent(activity, GoodsListActivity.class)
                            .putExtra(KEY_TITLE,category.getName())
                        .putExtra("orderCode",category.getItemList().get(0).getOrderCode())
                    );
                }else{
                    Utils.showToast(activity,"没有更多商品了");
                }

            }
        });

        MyGridView grid = (MyGridView) itemView.findViewById(R.id.grid);
        List<ShopBean.CategoryItem> list = new ArrayList<>();
        if (category.getItemList().size() > 4){
            list = category.getItemList().subList(0,4);
        }else{
            list = category.getItemList();
        }
        grid.setAdapter(new QuickAdapter<ShopBean.CategoryItem>(activity,R.layout.item_goods,list) {
            @Override
            protected void convert(BaseAdapterHelper helper, final ShopBean.CategoryItem item) {
                helper.setText(R.id.tv_goodsInfo,item.getGoodsName());
                helper.setText(R.id.tv_nowPrice, String.format("¥ %.2f" , item.getBasePrice()));
                helper.setVisible(R.id.isSale,item.getIsSale() != 0);
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context,GoodsDetailActivity.class);
                        intent.putExtra("goodsId", item.getMgiId());
                        context.startActivity(intent);
                    }
                });

                ImageView iv_goodsIcon = helper.getView(R.id.iv_goodsIcon);
                Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(RES_ID_GOODS_DEFAULT).into(iv_goodsIcon);
            }
        });
    }

    /**
     * imgLogo
     * txtTitle
     * hl
     * @param activity
     * @param itemView
     * @param _data
     */
    private void initCollocationItem(final Activity activity, View itemView, final MatchListBean.Detail _data) {
        if(_data == null){
            return;
        }
        ImageView imgLogo = (ImageView) itemView.findViewById(R.id.imgLogo);
        TextView txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
        imgLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(activity, MatchActivity.class);
                intent.putExtra(KEY_ID,_data.getMgtiId());
                activity.startActivity(intent);
            }
        });
        Picasso.with(activity).load(FileUtils.rePath(_data.getUrl())).placeholder(R.drawable.i_match).into(imgLogo);
        txtTitle.setText(_data.getSubjectsName());
        MagicIndicator indicator = (MagicIndicator) itemView.findViewById(R.id.hl);
        CommonNavigator navigator = new CommonNavigator(activity);
        CommonNavigatorAdapter adapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return _data.getGoodsList() == null ? 0 : _data.getGoodsList().size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                View contentView = LayoutInflater.from(context).inflate(R.layout.item_match_cell,null);
                commonPagerTitleView.setContentView(contentView);
                final MatchListBean.Item item = _data.getGoodsList().get(index);
                final TextView txtName = (TextView) contentView.findViewById(R.id.txtName);
                txtName.setText(item.getGoodsName());
                TextView txtPrice = (TextView) contentView.findViewById(R.id.txtPrice);
                txtPrice.setText(String.format("¥ %.2f" , item.getBasePrice()));
                ImageView imgItem = (ImageView) contentView.findViewById(R.id.imgItem);
                Picasso.with(activity).load(FileUtils.rePath(item.getUrl())).placeholder(RES_ID_GOODS_DEFAULT).into(imgItem);

                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        activity.startActivity(
                                new Intent(activity, GoodsDetailActivity.class)
                                        .putExtra("goodsId",item.getMgiId())
                        );
                    }
                });
                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        };
        navigator.setAdapter(adapter);
        indicator.setNavigator(navigator);
    }

    private void initCollocation(Activity activity, View itemView) {

    }

    private void initStyle(final Activity activity, View itemView, final List<ShopBean.Category> data) {

        MagicIndicator hl = (MagicIndicator) itemView.findViewById(R.id.hl);
        MagicIndicator indicator = (MagicIndicator) itemView.findViewById(R.id.hl);
        CommonNavigator navigator = new CommonNavigator(activity);
        CommonNavigatorAdapter adapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return data.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                CommonPagerTitleView commonPagerTitleView = new CommonPagerTitleView(context);
                View contentView = LayoutInflater.from(context).inflate(R.layout.item_shop_style,null);
                commonPagerTitleView.setContentView(contentView);
                final ShopBean.Category item = data.get(index);
                final TextView txtCN = (TextView) contentView.findViewById(R.id.txtCN);
                txtCN.setText(item.getName());
                TextView txtEN = (TextView) contentView.findViewById(R.id.txtEN);
                txtEN.setText(item.getNameEn());
                ImageView imgItem = (ImageView) contentView.findViewById(R.id.imgItem);
                Picasso.with(activity).load(FileUtils.rePath(item.getUrl())).placeholder(R.color.bg_mask).error(R.color.bg_mask).into(imgItem);

                commonPagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        activity.startActivity(new Intent(activity, GoodsListActivity.class)
                                .putExtra(KEY_TITLE,item.getName())
                                .putExtra("orderCode",data.get(index).getOrderCode()));
                    }
                });
                return commonPagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                return null;
            }
        };
        navigator.setAdapter(adapter);
        indicator.setNavigator(navigator);
    }
}

